home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: free space malloced to auto vars??
- Date: 03 Feb 1996 21:18:08 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Feb3141808@qcd.lanl.gov>
- References: <4eqf8m$smv@gsusgi1.gsu.edu>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: matmrlx@gsusgi1.gsu.edu's message of 1 Feb 1996 08:29:58 -0500
-
- In article <4eqf8m$smv@gsusgi1.gsu.edu> matmrlx@gsusgi1.gsu.edu
- (Michael R. Lauer) writes:
- <snip>
- The general question is: if you malloc space for a pointer with automatic
- storage, should/must you free that space before leaving that scope?
-
- I assume from your later example that you are talking about the scope
- of the pointer which holds the last or only copy of an address
- pointing to a malloc'ed region: not the scope where the malloc was
- actually done.
-
- Freeing it is a good idea :-) Technically, you don't need to free it
- ... if you don't free it, you continue to hold on to the space without
- any means of either using it or freeing it. I guess that's what you
- meant anyways.
-
- This seems obvious but it is giving me trouble. In this code:
-
- {
- struct dirent *dp;
- if ((dp = readdir() != NULL)
- {
- }
- if (dp) /* this seems to cause
- a segmentation fault */
- free(dp); /* when the enclosing function
- returns */
- }
-
- So readdir returns a pointer to the dirent structure--it is mallocing the
- space. When I leave this scope I AM done with dp and the struct.
- Don't I have to free(dp)? The "if(dp) free(dp)" at the end of the
- scope caused
- a segmentation fault when I returned from the enclosing function. It
- apparently works fine without the free(), but I am worried about memory
- leakage.
-
- Apart from the typo, the code fragment seems correct. The if(dp) part
- is not needed in an ANSI environment, pre-ANSI libraries sometimes
- crashed on free(NULL).
-
- Are you absolutely sure that readdir is malloc-ing the space? Why
- don't you ask in a group more suited to that question. Anything can
- happen if you free something that has not been malloced. Say,
- something called opendir() mallocs space and something called
- readdir() returns a pointer to somewhere in this malloced place. Then
- if you free the pointer returned by readdir(), all hell can break
- loose.
-
- Note that the only thing that can be freed is a pointer returned by
- malloc or calloc or realloc, not other pointers pointing inside a
- malloc'ed region.
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-